#


import time

import requests as requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from config import webdriverPath, hostUrl


class zyshgzbObj:
    i = 0
    item = None

    def set(self, item):
        self.i = item['currQuantity']
        self.list = []
        self.item = item
        self.citationList = []

    def run(self):
        # url_root = 'https://webofscience-clarivate-cn-s.era.lib.swjtu.edu.cn/wos/alldb/basic-search'
        # 创建ChromeOptions对象
        chrome_options = webdriver.ChromeOptions()
        # 禁止加载图片等资源
        chrome_options.add_argument("--disable-images")
        chrome_options.add_argument("--disable-plugins")
        chrome_options.add_argument("--disable-extensions")
        # 设置全屏参数
        chrome_options.add_argument("--start-maximized")

        # 创建WebDriver对象时传入ChromeOptions
        self.driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
        self.driver.get('https://www.zyshgzb.gov.cn/459388/index.html')  # 打开的页面

    def getList(self):

        self.driver.get('https://www.zyshgzb.gov.cn/459388/index.html')  # 打开的页面
        # 等待元素加载
        try:
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                  'body > div.ymain > div.content-common > div > div.content-box > div.list-wrap'))
            )
            print("元素加载成功")
        except Exception as e:
            print("元素加载失败:", e)
            self.driver.quit()
            return
        # 获取元素
        elements = self.driver.find_elements(By.CSS_SELECTOR,
                                             'body > div.ymain > div.content-common > div > div.content-box > div.list-wrap > ul > li')
        # 遍历元素
        for element in elements:
            # 获取元素的属性值
            a = element.find_element(By.CSS_SELECTOR, 'a')
            href = a.get_attribute('href')
            title = a.text
            date = element.find_element(By.CSS_SELECTOR, 'em').text
            print(title, href, date)
            requests.post(hostUrl + '/policy/save',
                          json={'documentName': title, 'accessionNo': href.replace('https://www.zyshgzb.gov.cn/', ''),
                                'url': href, 'date': date + ' 00:00:00',
                                'type': 'zyshgzb',
                                'status': '10'})
            # return

    def detail(self):
        while True:
            try:
                res = requests.post(hostUrl + '/policy/list', json={ 'type': 'zyshgzb'})
                if res.json().get('code') != 200:
                    break
                data = res.json().get('data')
                self.driver.get(data['url'])
                time.sleep(2)
                # 等待元素加载
                try:
                    # 或者等待直到某个元素可见
                    WebDriverWait(self.driver, 10).until(
                        EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                          'body > div.ymain > div.content-common > div > div.content-box > div > div > h1'))
                    )
                    print("元素加载成功")
                except Exception as e:
                    print("元素加载失败:", e)
                    continue

                # 获取元素
                fullText = self.driver.find_element(By.CSS_SELECTOR,
                                                    'body > div.ymain > div.content-common > div > div.content-box').get_attribute(
                    'innerHTML')
                fullText = fullText.replace('分享让更多人看到：', '')

                abstractInfo = self.driver.find_element(By.CSS_SELECTOR,
                                                        'body > div.ymain > div.content-common > div > div.content-box > div > div > div.article-context > p:nth-child(1)').text
                if len(abstractInfo) < 100:
                    abstractInfo = abstractInfo + '\n' + self.driver.find_element(By.CSS_SELECTOR,
                                                                                  'body > div.ymain > div.content-common > div > div.content-box > div > div > div.article-context > p:nth-child(2)').text
                if len(abstractInfo) < 100:
                    abstractInfo = abstractInfo + '\n' + self.driver.find_element(By.CSS_SELECTOR,
                                                                                  'body > div.ymain > div.content-common > div > div.content-box > div > div > div.article-context > p:nth-child(3)').text
                date = self.driver.find_element(By.CSS_SELECTOR,
                                                'body > div.ymain > div.content-common > div > div.content-box > div > div > div.article-info > span.date').text
                date = date.replace('年', '-').replace('月', '-').replace('日', ' ') + ':00'
                source = self.driver.find_element(By.CSS_SELECTOR,
                                                  'body > div.ymain > div.content-common > div > div.content-box > div > div > div.article-info > span.origin > a').text
                print(date, abstractInfo, source)
                data['fullText'] = fullText
                data['abstractInfo'] = abstractInfo
                data['date'] = date
                data['source'] = source
                data['status'] = '20'
                requests.post(hostUrl + '/policy/save',
                             json=data)
            except Exception as e:
                print(e)



if __name__ == '__main__':
    a = zyshgzbObj()
    a.run()
    # a.getList()
    a.detail()

    time.sleep(5)
